﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dir
{
    class CreateFile
    {
        public static void HTMLfile(string path, string path2, string ext) // или добавить path2 , а также 3-й параметр для стиля
        {
            if (File.Exists(path))
            {
                try
                {
                    path2 = path2 + @"\" + Path.GetFileNameWithoutExtension(path) + ".html";
                    StreamWriter sw = new StreamWriter(path2);
                    //StreamWriter sw = File.CreateText(path2);
                    sw.WriteLine(@"<html><body><style type=""text/css"">
                                   span.colorB {color:blue;}
                                   span.number {color:red;} 
                                   span.string {color:brown;} 
                                   span.comment {color:green;}
                                   </style><pre>");
                    StreamReader sr = new StreamReader(path);
                    string line = sr.ReadToEnd();

                    if (ext == ".cpp")
                        sw.WriteLine(C_Formatter.Parse(line));
                    else if (ext == ".pas")
                        sw.WriteLine(PAS_Formatter.Parse(line));
                    else
                        sw.WriteLine(CS_Formatter.Parse(line));

                    sw.WriteLine(@"</pre></body></html>");
                    sw.Close();
                    sr.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }

            }

        }

        public static void HTMLcssfile(string path, string path2, string ext) // или добавить path2 , а также 3-й параметр для стиля
        {
            if (File.Exists(path))
            {
                try
                {
                    path2 = path2 + @"\" + Path.GetFileNameWithoutExtension(path) + ".html";
                    StreamWriter sw = new StreamWriter(path2);
                    //StreamWriter sw = File.CreateText(path2);
                    sw.WriteLine(@"<html><body><style type=""text/css"">
                             span.colorB {color:blue;}
                             span.number {color:red;} 
                             span.string {color:brown;} 
                             span.comment {color:green;}
                             div.example {
                             counter-reset: item; /* Обнуляем счетчик списка */
                             background: #F5F5DC; /* Цвет фона */
                             /*border: 1px solid #333; *//* Параметры рамки */
                             }
                             div.example p {
                             margin: 0;
                             font-size: 12pt;
                             }
                             div.example p:before {
                             content: counter(item, decimal-leading-zero); /* Добавляем число */
                             background: #F4A460; /* Цвет фона под числами */
                             color: #fff; /* Цвет чисел */
                             padding: 4px; /* Поля слева и справа */
                             /*margin-right: 7px;*/
                             counter-increment: item; /* Задаем имя счетчика */
                             }
                             </style><pre>");
                    StreamReader sr = new StreamReader(path);
                    string line = sr.ReadToEnd();
                    if (ext == ".cpp")
                        sw.WriteLine(C_Formatter.Parse(line));
                    else if (ext == ".pas")
                        sw.WriteLine(PAS_Formatter.Parse(line));
                    else
                        sw.WriteLine(CS_Formatter.Parse(line));

                    sw.WriteLine(@"</pre></body></html>");
                    sw.Close();
                    sr.Close();
                    CSS_Style.Parse(path2);
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }

            }

        }
        public static void HTMLcode(string path, string path2, string ext)
        {
            if (File.Exists(path))
            {
                try
                {
                    path2 = path2 + @"\" + Path.GetFileNameWithoutExtension(path) + ".txt";
                    StreamWriter sw = File.CreateText(path2);
                    sw.WriteLine(@"<style type=""text/css"">
                             span.colorB {color:blue;}
                             span.number {color:red;} span.string {color:brown;} span.comment {color:green;}
                                           </style><pre>");

                    StreamReader sr = new StreamReader(path);
                    string line = sr.ReadToEnd();
                    if (ext == ".cpp")
                        sw.WriteLine(C_Formatter.Parse(line));
                    else if (ext == ".pas")
                        sw.WriteLine(PAS_Formatter.Parse(line));
                    else
                        sw.WriteLine(CS_Formatter.Parse(line));

                    sw.WriteLine(@"</pre>");
                    sw.Close();
                    sr.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }
            }
        }

        public static void IndexFile(List<string> filePathList, string pathIndex, bool txt)
        {

            pathIndex = pathIndex + @"\" + "index.html";

            StreamWriter sw = new StreamWriter(pathIndex, false, Encoding.Default);
            //            File.AppendAllText(pathIndex, @"<html><body><style type=""text/css"">
            //                             span.colorB {color:blue;}
            //                             span.number {color:red;} span.string {color:brown;} span.comment {color:green;}
            //                                           </style><pre>", Encoding.Default);
            sw.WriteLine(@"<html><body><style type=""text/css"">
                             span.colorB {color:blue;}
                             span.number {color:red;} 
                             span.string {color:brown;} 
                             span.comment {color:green;}
                             div.example {
                             counter-reset: item; /* Обнуляем счетчик списка */
                             background: #E0FFFF; /* Цвет фона */
                             /*border: 1px solid #333; *//* Параметры рамки */
                             }
                             div.example p {
                             margin: 0;
                             font-family: 'Times New Roman'; 
                             font-size: 12pt; 
                             }
                             div.example p:before {
                             content: counter(item, decimal-leading-zero); /* Добавляем число */
                             background: #0000FF; /* Цвет фона под числами */
                             color: #fff; /* Цвет чисел */
                             padding: 4px; /* Поля слева и справа */
                             /*margin-right: 7px;*/
                             counter-increment: item; /* Задаем имя счетчика */
                             }
                             </style><pre>");
            foreach (string path in filePathList)
            {
                if (File.Exists(path))
                {
                    try
                    {
                        string path2;
                        if (txt)
                            path2 = Path.GetDirectoryName(path) + @"\" + Path.GetFileNameWithoutExtension(path) + ".txt";
                        else
                            path2 = Path.GetDirectoryName(path) + @"\" + Path.GetFileNameWithoutExtension(path) + ".html";
                        sw.WriteLine(@"<A HREF=""file:///{0}"" target=""_blank"">{1}</A>", path2,
                            Path.GetFileNameWithoutExtension(path));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

            }
            sw.WriteLine(@"</pre></body></html>");
            sw.Close();
            CSS_Style.Parse(pathIndex);
        }
    }
}
